#include "i2cm.h"
#include "rtc_DS1307.h"

//-----------------------------------------------------------
// Init. RTC
//-----------------------------------------------------------
char DS1307_Init(void)
{
	unsigned char tmp, mode;

	if(!i2c_start(DEV_DS1307 + I2C_WRITE))
	{
		i2c_write(DS1307_REG_SEC);                 // Set address                    
		i2c_rep_start(DEV_DS1307 + I2C_READ);      // set device address and read mode
		tmp =  i2c_readAck();                      // read osz.
		mode =  i2c_readAck();                     // dummy read
		mode =  i2c_readNak();                     // read 24h mode
		i2c_stop();
		DS1307_Present = 1;   
		if ((tmp & 0x80) || (mode & 0x40))                            // osz. not running ??
		{
			i2c_start(DEV_DS1307 + I2C_WRITE);
			i2c_write(DS1307_REG_SEC);               // Set address                    
			i2c_write(0x00);                         // start osz + sec=0
			i2c_write(0x00);                         // min=0 
			i2c_write(0x00);                         // 24h-mode + hours=0
			i2c_write(0x06);                         // day 6=SA, 0=SO
			i2c_write(0x1);                          // date 
			i2c_write(0x4);                          // month
			i2c_write(0x06);                         // year
			i2c_write(0x00);                         // Squarewave output disabled
			i2c_stop();
		}
		return 0;
	}
	else 
	{ 
		DS1307_Present = 0;
		return -1;
	}
} 

//-----------------------------------------------------------
// Read date/time from RTC
//-----------------------------------------------------------
void DS1307_GetDateTime(unsigned char *day, unsigned char *month, unsigned char *year, unsigned char *hour, unsigned char *min, unsigned char *sec)
{
  unsigned char tmp;
   
   i2c_start_wait(DEV_DS1307 + I2C_WRITE);
   i2c_write(DS1307_REG_SEC);                // Set address                    
   i2c_rep_start(DEV_DS1307 + I2C_READ);     // set device address and read mode
   tmp =  i2c_readAck();              // read seconds
   (*sec) =  (tmp & 0x0f) + ((tmp >> 4) * 10);
   tmp =  i2c_readAck();                     // read minutes
   (*min) =  (tmp & 0x0f) + ((tmp >> 4) * 10);
   tmp =  i2c_readAck() & 0x3f;              // read hours (24h mode)
   (*hour) = (tmp & 0x0f) + (tmp >> 4) * 10;
   tmp =  i2c_readAck() & 0x07;              // read weekday - !!! not used !!!
   tmp =  i2c_readAck() & 0x3F;				 // read month day (1..31)
   (*day) = (tmp & 0x0f) + ((tmp >> 4) * 10);
   tmp =  i2c_readAck() & 0x1F;              // read month (1..12)
   (*month) = (tmp & 0x0f) + ((tmp >> 4) * 10);
   tmp =  i2c_readNak(); // -- NAK --- 		 // read year (00..99)
   (*year) = (tmp & 0x0f) + ((tmp >> 4) * 10);

  i2c_stop();
} 

//-----------------------------------------------------------
// Set date/time of RTC
//-----------------------------------------------------------
void DS1307_SetDateTime(unsigned char day, unsigned char month, unsigned char year, unsigned char hour, unsigned char min, unsigned char sec)
{
	i2c_start(DEV_DS1307 + I2C_WRITE);
	i2c_write(DS1307_REG_SEC);                    // Set address                    
	i2c_write((sec % 10)  | ((sec / 10)  <<4));   // set sec + start osz
	i2c_write((min % 10)  | ((min / 10)  <<4));   // set min
	i2c_write((hour % 10) | ((hour / 10) <<4));   // set hours + 24h mode
	i2c_stop();

	i2c_start(DEV_DS1307 + I2C_WRITE);
	i2c_write(DS1307_REG_DATE);                    // Set Date-Reg address
	i2c_write((day % 10)    | ((day / 10)   <<4)); // set month day (1..31)
	i2c_write((month % 10)  | ((month / 10) <<4)); // set month (1..12)
	i2c_write((year % 10)   | ((year / 10)  <<4)); // set year (00.99)
	i2c_stop();
} 
   
//-----------------------------------------------------------
// Set day of week
//-----------------------------------------------------------
void DS1307_SetWDay(unsigned char day)
{
  i2c_start(DEV_DS1307 + I2C_WRITE);
   i2c_write(DS1307_REG_DAY);                            // Set address                    
   i2c_write(day + 1); 										 // set day
  i2c_stop();
  
} 

//-----------------------------------------------------------
// Control Square wave output
//-----------------------------------------------------------
void DS1307_Output(unsigned char state)
{
  if(DS1307_Present)
  { i2c_start(DEV_DS1307 + I2C_WRITE);
    i2c_write(DS1307_REG_CTRL);                      // Set address                    
    i2c_write((state<<7)); 							// set output
   i2c_stop();
  }
} 

//-----------------------------------------------------------
// Control Square wave freq.
//-----------------------------------------------------------
void DS1307_Freq(unsigned char freq)
{
  if(DS1307_Present)
  { i2c_start_wait(DEV_DS1307 + I2C_WRITE);
    i2c_write(DS1307_REG_CTRL);                  // Set address                    
    i2c_write(freq);    							// set freq
    i2c_stop();
  }
} 
